package com.ibm.core.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

public abstract class DateTimeUtils
{
  public static String getCnDayWeekday(java.util.Date date)
  {
    String formatDate = DateFormat.getDateInstance(0)
      .format(date);
    return formatDate;
  }

  public static boolean isSameDay(Calendar cal1, Calendar cal2)
  {
    if ((cal1 == null) || (cal2 == null)) return false;
    return (cal1.get(0) == cal2.get(0)) && (cal1.get(1) == cal2.get(1)) && 
      (cal1
      .get(6) == cal2.get(6));
  }

  public static boolean isSameDay(java.util.Date date1, java.util.Date date2) {
    if ((date1 == null) || (date2 == null)) return false;
    Calendar cal1 = Calendar.getInstance();
    cal1.setTime(date1);
    Calendar cal2 = Calendar.getInstance();
    cal2.setTime(date2);
    return isSameDay(cal1, cal2);
  }

  public static boolean isSameInstant(java.util.Date date1, java.util.Date date2) {
    if ((date1 == null) || (date2 == null)) return false;
    return date1.getTime() == date2.getTime();
  }

  public static boolean isSameInstant(Calendar cal1, Calendar cal2) {
    if ((cal1 == null) || (cal2 == null)) return false;
    return cal1.getTime().getTime() == cal2.getTime().getTime();
  }

  public static java.util.Date addYears(java.util.Date date, int amount) {
    return add(date, 1, amount);
  }

  public static java.util.Date addMonths(java.util.Date date, int amount) {
    return add(date, 2, amount);
  }

  public static java.util.Date addWeeks(java.util.Date date, int amount) {
    return add(date, 3, amount);
  }

  public static java.util.Date addDays(java.util.Date date, int amount) {
    return add(date, 5, amount);
  }

  public static java.util.Date addHours(java.util.Date date, int amount) {
    return add(date, 11, amount);
  }

  public static java.util.Date addMinutes(java.util.Date date, int amount) {
    return add(date, 12, amount);
  }

  public static java.util.Date addSeconds(java.util.Date date, int amount) {
    return add(date, 13, amount);
  }

  public static java.util.Date addMilliseconds(java.util.Date date, int amount) {
    return add(date, 14, amount);
  }

  public static java.util.Date add(java.util.Date date, int calendarField, int amount) {
    if (date == null) throw new IllegalArgumentException("日期不能为空");

    Calendar c = Calendar.getInstance();
    c.setTime(date);
    c.add(calendarField, amount);
    return c.getTime();
  }

  public static String today() {
    return today("yyyy-MM-dd");
  }

  public static String now() {
    return today("yyyy-MM-dd HH:mm:ss SSS");
  }

  public static String nowTime() {
    return today("yyyyMMddHHmmss");
  }

  public static String today(String pattern) {
    if (pattern == null) throw new IllegalArgumentException("日期格式不能为空");
    SimpleDateFormat sdf = new SimpleDateFormat(pattern);
    String dt = sdf.format(new java.util.Date());
    return dt;
  }

  public static java.util.Date parseDate(String src) {
    return parse(src, "yyyy-MM-dd");
  }

  public static java.util.Date parseDatetime(String src) {
    return parse(src, "yyyy-MM-dd HH:mm:ss");
  }

  public static java.util.Date parse(String src, String pattern) {
    if (pattern == null) throw new IllegalArgumentException("日期格式不能为空");
    if ((src == null) || ("".equals(src))) return null; try
    {
      return new SimpleDateFormat(pattern).parse(src); } catch (ParseException ex) {
    }
    throw new IllegalArgumentException("日期格式转换出错,src=" + src + ",pattern=" + pattern);
  }

  public static String formatDate(java.util.Date src)
  {
    return format(src, "yyyy-MM-dd");
  }

  public static String formatDatetime(java.util.Date src) {
    return format(src, "yyyy-MM-dd HH:mm:ss");
  }

  public static String format(java.util.Date src, String pattern) {
    if (pattern == null) throw new IllegalArgumentException("日期格式不能为空");
    if (src == null) return null;
    return new SimpleDateFormat(pattern).format(src);
  }

  public static int getLastDayOfMonth(int y, int m) {
    boolean IsLeapYear = ((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0);
    int days = 0;
    switch (m) {
    case 2:
      if (IsLeapYear)
        days = 29;
      else days = 28;
      break;
    case 4:
    case 6:
    case 9:
    case 11:
      days = 30;
      break;
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    default:
      days = 31;
    }

    return days;
  }

  public static String getCnWeekDay()
  {
    return getCnWeekDay(new java.util.Date());
  }

  public static String getCnWeekDay(java.util.Date date)
  {
    String[] weekDays = { "日", "一", "二", "三", "四", "五", "六" };
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    int w = cal.get(7) - 1;
    if (w < 0) w = 0;
    return "星期" + weekDays[w];
  }

  public static java.util.Date getMaxDateTime()
  {
    return parseDatetime("9999-12-31 23:59:59");
  }

  public static java.util.Date getMaxDate()
  {
    return parseDate("9999-12-31");
  }

  public static String timeGo(java.util.Date startTime, java.util.Date finishTime)
  {
    long go = finishTime.getTime() - startTime.getTime();
    if (go < 1000L) {
      return go + "毫秒";
    }
    return go / 1000L + "秒";
  }

  public static String getDateTime()
  {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String dt = sdf.format(new java.util.Date());
    return dt;
  }

  public static String getDate()
  {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    String dt = sdf.format(new java.util.Date());
    return dt;
  }

  public static String getYear()
  {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    String dt = sdf.format(new java.util.Date());
    return dt.substring(0, 4);
  }

  public static String getDateTime(String pattern)
  {
    SimpleDateFormat sdf = new SimpleDateFormat(pattern);
    String dt = sdf.format(new java.util.Date());
    return dt;
  }

  public static String shortFmt(java.util.Date date)
  {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
    return sdf.format(date);
  }

  public static String dateTimeFmt(java.util.Date date)
  {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    return sdf.format(date);
  }

  public static String dateFmt(java.util.Date date)
  {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    return sdf.format(date);
  }

  public static String dateFmt(java.util.Date date, String pattern)
  {
    SimpleDateFormat sdf = new SimpleDateFormat(pattern);
    return sdf.format(date);
  }

  public static String shortFmt(java.util.Date date, String format)
  {
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    return sdf.format(date);
  }

  public static java.util.Date parse(String param)
  {
    java.util.Date date = null;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    try {
      date = sdf.parse(param); } catch (ParseException localParseException) {
    }
    return date;
  }

  public static java.util.Date parseShort(String param)
  {
    java.util.Date date = null;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    try {
      date = sdf.parse(param); } catch (ParseException localParseException) {
    }
    return date;
  }

  public static int getDays(int year, int month)
  {
    int[] numberMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    int numberOfMonth = numberMonth[(month - 1)];
    if ((month == 2) && (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0))) {
      numberOfMonth++;
    }
    return numberOfMonth;
  }

  public static int[] getLastDay(int year, int month, int day)
  {
    if ((year < 1000) || (year > 3000) || (month <= 0) || (day <= 0) || (day > 31) || (month > 12)) return null;
    int[] date = new int[3];
    if (day == 1) {
      if (month == 1) {
        date[0] = (year - 1);
        date[1] = 12;
        date[2] = getDays(date[0], date[1]);
      } else {
        date[0] = year;
        date[1] = (month - 1);
        date[2] = getDays(date[0], date[1]);
      }
    } else {
      date[0] = year;
      date[1] = month;
      date[2] = (day - 1);
    }
    return date;
  }

  public static int[] getNextDay(int year, int month, int day)
  {
    if ((year < 1000) || (year > 3000) || (month <= 0) || (day <= 0) || (day > 31) || (month > 12)) return null;
    int[] date = new int[3];
    if (isLastDayOftheMonth(year, month, day)) {
      if (month == 12) {
        date[0] = (year + 1);
        date[1] = 1;
        date[2] = 1;
      } else {
        date[0] = year;
        date[1] = (month + 1);
        date[2] = 1;
      }
    } else {
      date[0] = year;
      date[1] = month;
      date[2] = (day + 1);
    }
    return date;
  }

  public static int getYear(String date)
    throws NumberFormatException
  {
    String m = date.substring(0, 4);
    return Integer.parseInt(m);
  }

  public static int getMonth(String date)
    throws NumberFormatException
  {
    String m = date.substring(date.indexOf("-") + 1, date.indexOf("-") + 3);
    return Integer.parseInt(m);
  }

  public static int getDay(String date)
    throws NumberFormatException
  {
    String m = date.substring(date.lastIndexOf("-") + 1);
    return Integer.parseInt(m);
  }

  public static int getQuarter(String date)
    throws NumberFormatException
  {
    String q = date.substring(date.lastIndexOf("#") + 1);
    return Integer.parseInt(q);
  }

  public static int getPeriod(String date)
    throws NumberFormatException
  {
    String p = date.substring(date.lastIndexOf("$") + 1);
    return Integer.parseInt(p);
  }

  public static java.util.Date monthAdd(java.util.Date src, int add)
  {
    Calendar ca = Calendar.getInstance();
    ca.setTime(src);
    ca.set(2, ca.get(2) + add);
    return ca.getTime();
  }

  public static java.util.Date dayAdd(java.util.Date src, int add)
  {
    Calendar ca = Calendar.getInstance();
    ca.setTime(src);
    ca.set(6, ca.get(6) + add);
    return ca.getTime();
  }

  public static int[] getLastMonth(int year, int month)
  {
    if ((year < 1000) || (year > 3000) || (month <= 0) || (month > 12)) return null;
    int YEAR = 0;
    int MONTH = 1;
    int[] date = new int[2];
    if (month == 1) {
      date[1] = 12;
      date[0] = (year - 1);
    } else {
      date[1] = (month - 1);
      date[0] = year;
    }
    return date;
  }

  public static ArrayList setDailyPhase(String begindate, String enddate)
    throws NumberFormatException
  {
    if ((begindate == null) || (enddate == null)) return null;
    int beginYear = getYear(begindate);
    int endYear = getYear(enddate);
    int beginMonth = getMonth(begindate);
    int endMonth = getMonth(enddate);
    int beginDay = getDay(begindate);
    int endDay = getDay(enddate);
    int i = beginYear;
    int j = beginMonth;
    int k = beginDay;
    ArrayList dailyArray = new ArrayList();
    while (true) {
      if (k > getDays(i, j)) {
        k = 1;
        j++;
        if (j > 12) {
          j = 1;
          i++;
        }
      }
      dailyArray.add(k + "/" + j + "/" + i);
      if ((i == endYear) && (j == endMonth) && (k == endDay)) break;
      k++;
    }
    return dailyArray;
  }

  public static ArrayList setMonthlyPhase(String begindate, String enddate)
    throws NumberFormatException
  {
    if ((begindate == null) || (enddate == null)) return null;
    int beginYear = getYear(begindate);
    int endYear = getYear(enddate);
    int beginMonth = getMonth(begindate);
    int endMonth = getMonth(enddate);
    int i = beginYear;
    int j = beginMonth;
    ArrayList dailyArray = new ArrayList();
    while (true) {
      if (j > 12) {
        j = 1;
        i++;
      }
      dailyArray.add(j + "/" + i);
      if ((i == endYear) && (j == endMonth)) break;
      j++;
    }
    return dailyArray;
  }

  public static ArrayList setYearlyPhase(String begindate, String enddate)
    throws NumberFormatException
  {
    if ((begindate == null) || (enddate == null)) return null;
    int beginYear = getYear(begindate);
    int endYear = getYear(enddate);
    ArrayList dailyArray = new ArrayList();
    for (int i = beginYear; i <= endYear; i++) {
      dailyArray.add(i);
    }
    return dailyArray;
  }

  public static ArrayList setQuarterlyPhase(String begindate, String enddate)
    throws NumberFormatException
  {
    if ((begindate == null) || (enddate == null)) return null;
    int beginYear = getYear(begindate);
    int endYear = getYear(enddate);
    int beginQuarter = getQuarter(begindate);
    int endQuarter = getQuarter(enddate);
    int i = beginYear;
    int j = beginQuarter;
    ArrayList dailyArray = new ArrayList();
    while (true) {
      if (j > 4) {
        j = 1;
        i++;
      }
      dailyArray.add(i + "年" + j + "季度");
      if ((i == endYear) && (j == endQuarter)) break;
      j++;
    }
    return dailyArray;
  }

  public static ArrayList setPeriodPhase(String begindate, String enddate)
    throws NumberFormatException
  {
    if ((begindate == null) || (enddate == null)) return null;
    int beginYear = getYear(begindate);
    int endYear = getYear(enddate);
    int beginMonth = getMonth(begindate);
    int endMonth = getMonth(enddate);
    int beginPeriod = getPeriod(begindate);
    int endPeriod = getPeriod(enddate);
    int i = beginYear;
    int j = beginMonth;
    int k = beginPeriod;
    ArrayList dailyArray = new ArrayList();
    while (true) {
      if (k > 3) {
        k = 1;
        j++;
        if (j > 12) {
          j = 1;
          i++;
        }
      }
      String str = "";
      switch (k) {
      case 1:
        str = "上旬";
        break;
      case 2:
        str = "中旬";
        break;
      case 3:
        str = "下旬";
        break;
      }

      dailyArray.add(i + "年" + j + "月" + str);
      if ((i == endYear) && (j == endMonth) && (k == endPeriod)) break;
      k++;
    }
    return dailyArray;
  }

  public static String formatHourMinite(float hm)
  {
    String s = String.valueOf(hm);
    s = s.substring(0, s.indexOf(".")) + ":" + s.substring(s.indexOf(".") + 1, s.length());
    return s;
  }

  public static boolean isLastDayOftheMonth(int y, int m, int d)
  {
    return getLastDayOfMonth(y, m) == d;
  }

  public static boolean isDayIn(java.util.Date min, java.util.Date max, java.util.Date dt)
  {
    return (max.getTime() - dt.getTime() >= 0L) && (min.getTime() - dt.getTime() <= 0L);
  }

  public static java.sql.Date utilDateToSqlDate(java.util.Date dDate)
  {
    if (dDate != null) {
      java.sql.Date sqlDate = new java.sql.Date(dDate.getTime());
      return sqlDate;
    }
    return null;
  }

  public static String fmtDate(java.util.Date src, String pattern) {
    SimpleDateFormat sdf = new SimpleDateFormat(pattern);
    return sdf.format(src);
  }

  public static String fmtDate(java.util.Date src) {
    return fmtDate(src, "yyyy-MM-dd");
  }

  public static java.util.Date parseDate(String src, String pattern) {
    SimpleDateFormat sdf = new SimpleDateFormat(pattern);
    try {
      return sdf.parse(src); } catch (ParseException e) {
    }
    throw new RuntimeException("解析日期出错：src=" + src + ",pattern=" + pattern);
  }

  public static java.util.Date getCurrentDate()
  {
    return new java.util.Date();
  }

  public static java.util.Date getCurrentDay() {
    return parseDate(fmtDate(new java.util.Date()), "yyyy-MM-dd");
  }
}